#include "threadedimageprovider.h"
#include <QtConcurrent/QtConcurrent>
#include <QDebug>


ThreadedImageProvider::ThreadedImageProvider(QObject *parent) :
    QObject(parent), QQuickImageProvider(QQuickImageProvider::Pixmap)
{}

QPixmap ThreadedImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
    Q_UNUSED(requestedSize);
    if (m_fetched.isNull()) {
        QPixmap pixmap(id);
        *size = pixmap.size();
//        qDebug() << "simple load";
        return pixmap;
    } else {
        *size = m_fetched.size();
//        qDebug() << "thread load";
        return QPixmap::fromImage(m_fetched);
    }
}

void ThreadedImageProvider::fetch(const QString &id)
{
    QtConcurrent::run(this, &ThreadedImageProvider::_fetch, id);
}

void ThreadedImageProvider::_fetch(const QString &id)
{
    m_fetched = QImage();
    m_fetched.load(id);
}
